﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system.Tools;
using bdnt_pets_system_Entity;
using System.Windows.Data;
using bdnt_pets_system.Resources;

namespace bdnt_pets_system.PageViews.Failures
{
    public partial class FailureInfoManagerControl : BaseUserControl
    {
        DataServiceReference.DataServiceClient dataClient;
        int pageSize = SessionManager.PageSize;
        int currentPage = 1;

        public FailureInfoManagerControl()
        {
            InitializeComponent();
            dataClient = new DataServiceReference.DataServiceClient();
            InitData();
            InitEvent();
            QueryData();
        }
        Tbl_FailureInfo CuTbl_FailureInfo
        {
            get { return this.DataContext as Tbl_FailureInfo; }
            set { this.DataContext = value; }
        }

        //默认查询条件
        Tbl_FailureInfo selectFailureInfo = new Tbl_FailureInfo()
        {
            IsValidate = false,
            PartNo = MyStrings.qsrbjbh,
            PartName = MyStrings.qsrbjmc,
            FailureName = MyStrings.qsrgz
        };

        void InitData()
        {
            tbPartNo.Foreground = tbPartName.Foreground =
                tbFailureName.Foreground = new SolidColorBrush(Colors.Gray);

            tbPartNo.SetBinding(TextBox.TextProperty, new Binding("PartNo") { Mode = BindingMode.TwoWay });
            tbPartNo.Tag = selectFailureInfo.PartNo;
            tbPartName.SetBinding(TextBox.TextProperty, new Binding("PartName") { Mode = BindingMode.TwoWay });
            tbPartName.Tag = selectFailureInfo.PartName;

            tbFailureName.SetBinding(TextBox.TextProperty, new Binding("FailureName") { Mode = BindingMode.TwoWay });
            tbFailureName.Tag = selectFailureInfo.FailureName;


            CuTbl_FailureInfo = new Tbl_FailureInfo()
            {
                IsValidate = false,
                PartNo = selectFailureInfo.PartNo,
                PartName = selectFailureInfo.PartName,
                FailureName = selectFailureInfo.FailureName
            };
        }

        void InitEvent()
        {
            btnNew.Click += new RoutedEventHandler(btnNew_Click);
            btnSelect.Click += new RoutedEventHandler(btnSelect_Click);
            dataClient.GetFailureInfoListByConditionCompleted += new EventHandler<DataServiceReference.GetFailureInfoListByConditionCompletedEventArgs>(dataClient_GetFailureInfoListByConditionCompleted);
            dataClient.GetFailureInfoListByConditionCountCompleted += new EventHandler<DataServiceReference.GetFailureInfoListByConditionCountCompletedEventArgs>(dataClient_GetFailureInfoListByConditionCountCompleted);
            dataClient.DeleteCuFailureInfoCompleted += new EventHandler<DataServiceReference.DeleteCuFailureInfoCompletedEventArgs>(dataClient_DeleteCuFailureInfoCompleted);
            failurePageC.dpPage.PageIndexChanged += new EventHandler<EventArgs>(dpPage_PageIndexChanged);             
        }

        #region 查询与分页

        //数据库查询条件对象
        Tbl_FailureInfo selecV = new Tbl_FailureInfo();

        void QueryData()
        {
            selecV = new Tbl_FailureInfo()
            {
                IsValidate = false,
                PartNo = CuTbl_FailureInfo.PartNo == selectFailureInfo.PartNo ? "" : CuTbl_FailureInfo.PartNo,
                PartName = CuTbl_FailureInfo.PartName == selectFailureInfo.PartName ? "" : CuTbl_FailureInfo.PartName,
                FailureName = CuTbl_FailureInfo.FailureName == selectFailureInfo.FailureName ? "" : CuTbl_FailureInfo.FailureName
            };
            dataClient.GetFailureInfoListByConditionCountAsync(selecV);
        }

        void dataClient_GetFailureInfoListByConditionCountCompleted(object sender, DataServiceReference.GetFailureInfoListByConditionCountCompletedEventArgs e)
        {
            failurePageC.LoadPages(e.Result, pageSize);
        }

        void dpPage_PageIndexChanged(object sender, EventArgs e)
        {
            currentPage = failurePageC.PageIndex + 1;
            dataClient.GetFailureInfoListByConditionAsync(selecV, pageSize, currentPage);
        } 

        void dataClient_GetFailureInfoListByConditionCompleted(object sender, DataServiceReference.GetFailureInfoListByConditionCompletedEventArgs e)
        {
            gdList.ItemsSource = e.Result;
        }

        #endregion

        void dataClient_DeleteCuFailureInfoCompleted(object sender, DataServiceReference.DeleteCuFailureInfoCompletedEventArgs e)
        {
            if (e.Result)
                QueryData();
        }
         

        void btnSelect_Click(object sender, RoutedEventArgs e)
        {
            QueryData();
        }

        void btnNew_Click(object sender, RoutedEventArgs e)
        {
            var cupage = new NewFailureInfoWindow(new Tbl_FailureInfo());
            cupage.Show();
            cupage.Closed += new EventHandler(cupage_Closed);
        }

        void cupage_Closed(object sender, EventArgs e)
        {
            if ((sender as ChildWindow).DialogResult == true)
                QueryData();
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            var cuObj = (sender as HyperlinkButton).Tag as Tbl_FailureInfo;
            var cupage = new NewFailureInfoWindow(cuObj);
            cupage.Show();
            cupage.Closed += new EventHandler(cupage_Closed);
        }

        private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show(MyStrings.qdyscm,MyStrings.ts, MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                return;
            var cuObj = (sender as HyperlinkButton).Tag as Tbl_FailureInfo;
            dataClient.DeleteCuFailureInfoAsync(cuObj.Id);
        }
    }
}
